package com.migu.rbac.controller;

import com.migu.rbac.model.SysUser;
import com.migu.rbac.service.AuthService;
import com.migu.rbac.service.SysMenuService;
import com.migu.rbac.service.SysUserRoleService;
import com.migu.rbac.service.SysUserService;
import com.migu.rbac.util.CheckPassword;
import com.migu.rbac.util.UserUtils;
import com.migu.rbac.vo.GlobalConst;
import com.migu.rbac.vo.Response;
import com.migu.rbac.vo.ResultResponseEntity;
import com.migu.rbac.vo.page.UserPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhaojie
 * @Date: Created in 15:31 2018/4/24
 * @Description: 在Service 的实现类中添加业务缓存 即可将数据保存到缓存 下次根据key取缓存
 * @Modified By:
 */
@RestController
@RequestMapping(path = "/api/v1/sys")
public class UserController {

    @Autowired
    SysMenuService sysMenuService;


    @Autowired
    private SysUserRoleService userRoleService;

    @Autowired
    private SysUserService userService;

    //查询用户对应角色
    @GetMapping("/getUserRole/{userid}")
    public Response getUserRole(@PathVariable Integer userid) {
        Response result = null;
        try {
            result = Response.success(userRoleService.selectRolesByUserId(userid));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //分配用户角色
    @PostMapping(value = "/saveUserRole", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response saveUserRole(@RequestParam("userid") int userid,
                                 @RequestBody int[] roles) {
        Response result = null;
        try {
            int i = userRoleService.updateUserRole(userid, roles);
            if (i > 0) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "分配失败");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //新增用户
    @PostMapping(value = "/addUser")
    public Response addUser(@RequestBody SysUser sysUser) {
        Response result = null;
        try {
            Response check = CheckPassword.check(sysUser.getPassword(), sysUser.getLoginname());
            if (check.getCode() != Response.success().getCode()) {
                return check;
            }
            if (userService.saveUser(sysUser)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "新增失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //修改用户
    @PutMapping(value = "/updateUser")
    public Response updateUser(@RequestBody SysUser sysUser) {
        Response result = null;
        try {
            if (!StringUtils.isEmpty(sysUser.getPassword())) {
                Response check = CheckPassword.check(sysUser.getPassword(), sysUser.getLoginname());
                if (check.getCode() != Response.success().getCode()) {
                    return check;
                }
            }
            if (userService.updateUser(sysUser)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "修改失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //删除用户
    @DeleteMapping(value = "/deleteUser/{userid}")
    public Response deleteUser(@PathVariable int userid) {
        Response result = null;
        try {
            if (userService.deleteUser(userid)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "删除用户失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //按条件查询所有用户 分页
    @PostMapping("/user-page")
    @ResponseBody
    public Response userPage(@RequestBody UserPage userPage) {
        Response result = null;
        try {
            result = Response.success(userService.selectUserByExample(userPage));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //根据用户id查询用户
    @GetMapping("/getUserById/{userid}")
    public Response getUserById(@PathVariable Integer userid) {
        Response result = null;
        try {
            result = Response.success(userService.findUserByUserId(userid));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }



}
